<?php

namespace Server\Controller;
use Think\Controller\HproseController;
class BaseController extends HproseController {

	/**
	 * 创建附件信息入库
	 * @param  string 	$url 		附件url信息
	 * @return json 					如果成功，返回success=1,attachid,title,size
	 *								如果失败，返回success=0,errorcode,info
	 **/
	public function getAttachmentInfo($url){
		//检验参数信息是否正确（不能为空）
		if(trim($url)==''){
			//参数信息不完整，返回错误
			$arr  =  array('success' =>0,'errorcode'=>-1,'info'=>'读取附件信息失败，参数信息不完整！');
			$str = json_encode($arr, JSON_UNESCAPED_UNICODE);
			return $str;
			die;
		}

		//保存到数据库
		$ret = M('attach')->where(array('url'=>$url))->field('id,title')->find();
		if($ret){
			//验证成功
			$arr  =  array('success' =>1,'attachid'=>$ret['id'],'title'=>$ret['title']);
			$str = json_encode($arr, JSON_UNESCAPED_UNICODE);
			return $str;
			die;
		}else{
			//验证失败
			$arr  =  array('success' =>0,'errorcode'=>304,'info'=>'读取附件信息失败，数据不存在！');
			$str = json_encode($arr, JSON_UNESCAPED_UNICODE);
			return $str;
			die;

		}


	}
	/**
	 * 创建附件信息入库
	 * @param  string 	$token 		用户token
	 * @param  string    $title		附件标题
	 * @param  string    $type		附件后缀
	 * @param  string    $url		附件相对路径
	 * @return json 					如果成功，返回success=1
	 *								如果失败，返回success=0
	 **/
	public function createAttachmentInfo($token,$title,$type,$url){
		//检验参数信息是否正确（不能为空）
		if(trim($title)==''||trim($type)==''||trim($url)==''||trim($title)==''){
			//参数信息不完整，返回错误
			$arr  =  array('success' =>0,'errorcode'=>-1,'info'=>'保存失败，参数信息不完整！');
			$str = json_encode($arr, JSON_UNESCAPED_UNICODE);
			return $str;
			die;
		}
		//检验用户有效性
		$userarr = getUserFromToken($token);
		if($userarr){
			//用户信息获取成功
			$uid = $userarr["id"];
		}else{
			$uid = 0;
		}

		//保存到数据库
		$attachdata['title'] 		= $title;
		$attachdata['type'] 		= $type;
		$attachdata['url']			= $url;
		$attachdata['user_id'] 			= $uid;
		$attachdata['createtime'] 	= date('Y-m-d H:i:s', time());

		$ret = M('attach')->data($attachdata)->add();
		if($ret>0){
			//验证成功
			$arr  =  array('success' =>1);
			$str = json_encode($arr, JSON_UNESCAPED_UNICODE);
			return $str;
			die;
		}else{
			//验证失败
			$arr  =  array('success' =>0,'errorcode'=>300,'info'=>'保存失败，数据存储失败！');
			$str = json_encode($arr, JSON_UNESCAPED_UNICODE);
			return $str;
			die;

		}
	}

}